package org.springframework;

import org.junit.jupiter.api.Test;
import org.springframework.bean.User;
import org.springframework.context.event.SimpleApplicationEventMulticaster;
import org.springframework.listeners.OrderEvent;

public class MulticasterTest {

	@Test
	public void testMulticaster() {
		//1、创建一个事件发不起
		SimpleApplicationEventMulticaster multicaster = new SimpleApplicationEventMulticaster();

		//2、添加监听器
		multicaster.addApplicationListener(event -> {
			System.out.println("监听到事件1：" + event);
		});
		multicaster.addApplicationListener(event -> {
			System.out.println("监听到事件2：" + event);
		});

		User user = new User();
		user.setName("张三");
		user.setAge(18);
		OrderEvent orderEvent = new OrderEvent(user);

		//3、发布事件
		multicaster.multicastEvent(orderEvent);

		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			throw new RuntimeException(e);
		}
	}
}
